﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace comdiinfo
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args[0] != null)
            {
                string sourcePathFilename = args[0].ToString();
                String[] pathFileNames;
                String path = Path.GetDirectoryName(sourcePathFilename);
                if (path == "")
                {
                    path = Directory.GetCurrentDirectory();
                }
                String searchPattern = Path.GetFileName(sourcePathFilename);
                if ((searchPattern.IndexOf("*") > -1) || (searchPattern.IndexOf("?") > -1))
                {
                    pathFileNames = Directory.GetFiles(path, searchPattern);
                }
                else
                {
                    pathFileNames = new String[1];
                    pathFileNames[0] = Path.Combine(path, searchPattern);
                }
                foreach (String fn in pathFileNames)
                {
                    byte[] dataBuffer = null;
                    try
                    {
                        using (FileStream fsSource = new FileStream(fn, FileMode.Open, FileAccess.Read))
                        {
                            dataBuffer = new byte[fsSource.Length];
                            fsSource.Read(dataBuffer, 0, (int)fsSource.Length);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(String.Format("Could not read the file {0} ({1})"
                            , fn.ToString()
                            , ex.ToString()
                        ));
                    }
                    if (dataBuffer != null)
                    {
                        //G64DiskImage G64DI = new G64DiskImage(dataBuff2er);                        
                        AnalyseG64Image(dataBuffer, Path.GetFileName(fn), args);                        
                    }
                }
            }
            // Keep the console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
        static void ShowUsage()
        {
            Console.WriteLine("Usage: comdiinfo ImageFileName|ImageFileNameSearchPattern <switches>");
            Console.WriteLine("");
            Console.WriteLine("-i    Show info");
            Console.WriteLine("-a    Show area info");
            Console.WriteLine("-t    Show track info");
            Console.WriteLine("-c    Check all Image Elements");
        }
        static void AnalyseG64Image(byte[] dataBuffer, String fileName,string[] args)
        { 
            var g64DiskImage = new G64DiskImage(dataBuffer);
            Boolean firstArg = true;
            foreach (string argText in args)
            {
                if (firstArg)
                {
                    firstArg = false; // Skip Filename
                }
                else
                {
                    switch (argText)
                    {
                        case "-i":
                            g64DiskImage.ShowInfo();
                            break;
                        case "-a":
                            g64DiskImage.ShowAreaInfo();
                            break;
                        case "-t":
                            g64DiskImage.ShowTrackInfo();
                            break;
                        case "-c":
                            Console.Write(g64DiskImage.ShowInfo2(fileName));
                            break;
                        default:
                            Console.WriteLine(String.Format("switch unknown ({0})", argText));
                            break;
                    }

                }
            }
        }
    }
}
